import Vue from 'vue'
import Router from 'vue-router'
import store from './../store'
import tools from './../tools'

Vue.use(Router)

let router = new Router({
  routes: [{
      path: '/',
      name: 'home',
      meta: {
        title: 'Home'
      },
      component: resolve => { require(['@/components/Home'], resolve); }
    },
    {
      path: '/login',
      name: 'login',
      meta: {
        title: 'Login'
      },
      component: resolve => { require(['@/components/Login'], resolve); }
    },
    {
      path: '/browse',
      name: 'browse',
      meta: {
        title: 'Browse'
      },
      component: resolve => { require(['@/components/Browse'], resolve); }
    },
    {
      path: '/connections',
      name: 'connections',
      meta: {
        title: 'Connections'
      },
      component: resolve => { require(['@/components/Connections'], resolve); }
    },
    {
      path: '/me',
      name: 'me',
      meta: {
        title: 'Me'
      },
      component: resolve => { require(['@/components/Me'], resolve); }
    },
    {
      path: '/userInfo',
      name: 'userInfo',
      meta: {
        title: 'UserInfo'
      },
      component: resolve => { require(['@/components/UserInfo'], resolve); }
    }
  ]
});


// simple history management
tools.removeSessionStoreage('historyCount');
tools.removeSessionStoreage('historyList');

tools.setSessionStorage('historyList', JSON.stringify({}));
const historyList = tools.getObjSessionStorage('historyList');
historyList['/'] = 1;
tools.setSessionStorage('historyList', JSON.stringify(historyList));


let historyCount = tools.getSessionStorage('historyCount') * 1 || 1
let isPush = false
let endTime = Date.now()
let methods = ['push', 'go', 'replace', 'forward', 'back']

document.addEventListener('touchend', () => {
  endTime = Date.now()
})
methods.forEach(key => {
  let method = router[key].bind(router)
  router[key] = function(...args) {
    isPush = true
    method.apply(null, args)
  }
})

router.beforeEach(function(to, from, next) {
  store.commit('updateLoadingStatus', { isLoading: true })

  const toIndex = historyList[to.path]
  const fromIndex = historyList[from.path]

  if (toIndex) {
    if (!fromIndex || parseInt(toIndex, 10) > parseInt(fromIndex, 10) || (toIndex === '0' && fromIndex === '0')) {
      store.commit('updateDirection', { direction: 'forward' })
    } else {
      // 判断是否是ios左滑返回
      if (!isPush && (Date.now() - endTime) < 377) {
        store.commit('updateDirection', { direction: '' })
      } else {
        store.commit('updateDirection', { direction: 'reverse' })
      }
    }
  } else {
    ++historyCount
    tools.setSessionStorage('historyCount', historyCount)
    to.path !== '/' && (historyList[to.path] = historyCount)
    tools.setSessionStorage('historyList', JSON.stringify(historyList));
    store.commit('updateDirection', { direction: 'forward' })
  }

  if (/\/http/.test(to.path)) {
    let url = to.path.split('http')[1]
    window.location.href = `http${url}`
  } else {
    next()
  }
})

router.afterEach(function(to) {
  isPush = false
  store.commit('updateLoadingStatus', { isLoading: false })
})


export default router;
